!     Copyright (C) 2010 Benjamin Piaud
!
!     LIMBES is free software; you can redistribute it and/or modify
!     it under the terms of the GNU General Public License as published by
!     the Free Software Foundation; either version 3, or (at your option)
!     any later version.

!     LIMBES is distributed in the hope that it will be useful,
!     but WITHOUT ANY WARRANTY; without even the implied warranty of
!     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
!     GNU General Public License for more details.

!     You should have received a copy of the GNU General Public License
!     along with LIMBES; if not, see <http://www.gnu.org/licenses/>

!     subroutine LIMBES_macro computes at the node (xi,yi) the macroscopic fields
!     i.e the moments of the distribution function.
!     LIMBES_rho(1:LIMBES_nmax,1:LIMBES_nmax) -> rho (real*8)
!     LIMBES_ux(1:LIMBES_nmax,1:LIMBES_nmax) -> ux (real*8)
!     LIMBES_uy(1:LIMBES_nmax,1:LIMBES_nmax) -> uy (real*8)
!     LIMBES_T(1:LIMBES_nmax,1:LIMBES_nmax) -> T (real*8)
!     needed arguments
!     integer*8 xi,yi node coordinate


subroutine LIMBES_macro(ni)
  use LIMBES_mod_var
  use LIMBES_mod_fluid_config
  implicit none
  integer*8 :: ni
  real*8 :: rho,ux,uy,RT

  rho=sum(LIMBES_w(:)*LIMBES_f(ni,:))
  ux=sum(LIMBES_w(:)*LIMBES_vx(:)*LIMBES_f(ni,:))
  uy=sum(LIMBES_w(:)*LIMBES_vy(:)*LIMBES_f(ni,:))
  ux=ux/rho
  uy=uy/rho

  select case (SIMUL_thermal)
  case("thermal")
     RT=sum(LIMBES_w(:)*0.5d+0*((LIMBES_vx(:)-ux)**two + (LIMBES_vy(:)-uy)**two)*LIMBES_f(ni,:))
     RT=RT/rho
  case("isothermal")
     RT=FLUID_RT0
  case default
     print*,'error from LIMBES_macro:thermal/isothermal choice not known'
     print*,'SIMUL_thermal=',SIMUL_thermal
     stop

  end select

  LIMBES_rho(ni)=rho
  LIMBES_ux(ni)=ux
  LIMBES_uy(ni)=uy
  LIMBES_RT(ni)=RT

end subroutine LIMBES_macro
